package com.triones.api.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.triones.api.entity.AndroidVersionMgtDO;
import com.triones.api.service.AndroidVersionMgtService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 安卓版本控制
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/androidVersionMgt")
public class AndroidVersionMgtController extends BaseController {

    private final AndroidVersionMgtService androidVersionMgtService;

    /**
     * 获取安卓版本列表
     */
    @SaCheckPermission("system:androidVersionMgt:pageList")
    @GetMapping("/list")
    public TableDataInfo<AndroidVersionMgtDO> list(AndroidVersionMgtDO dto, PageQuery pageQuery) {
        return androidVersionMgtService.selectPageList(dto, pageQuery);
    }

    /**
     * 根据安卓版本编号获取详细信息
     */
    @SaCheckPermission("system:androidVersionMgt:query")
    @GetMapping(value = "/{id}")
    public R<AndroidVersionMgtDO> getInfo(@PathVariable Long id) {
        return R.ok(androidVersionMgtService.selectById(id));
    }

    /**
     * 新增安卓版本
     */
    @SaCheckPermission("system:androidVersionMgt:add")
    @PostMapping
    public R<Void> add(@Validated @RequestBody AndroidVersionMgtDO dto) {
        if (!androidVersionMgtService.checkVersionNoUnique(dto)) {
            return R.fail("新增版本'" + dto.getVersionNo() + "'失败，版本号已存在");
        }
        return toAjax(androidVersionMgtService.insert(dto));
    }

    /**
     * 修改安卓版本
     */
    @SaCheckPermission("system:androidVersionMgt:edit")
    @PutMapping
    public R<Void> edit(@Validated @RequestBody AndroidVersionMgtDO dto) {
        if (!androidVersionMgtService.checkVersionNoUnique(dto)) {
            return R.fail("修改版本'" + dto.getVersionNo() + "'失败，版本号已存在");
        }
        return toAjax(androidVersionMgtService.update(dto));
    }

    /**
     * 删除安卓版本
     *
     * @param id
     */
    @SaCheckPermission("system:androidVersionMgt:remove")
    @DeleteMapping("/{id}")
    public R<Void> removeData(@PathVariable Long id) {
        androidVersionMgtService.removeData(id);
        return R.ok();
    }
}
